package com.hkts.naturalgas.dictionaries.service.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hkts.naturalgas.datahandler.constance.EnviromentConstance;
import com.hkts.naturalgas.dictionaries.entity.HktsEnvironmentProtectionItem;
import com.hkts.naturalgas.dictionaries.entity.HktsFieldControl;
import com.hkts.naturalgas.dictionaries.mapper.HktsEnvironmentProtectionItemMapper;
import com.hkts.naturalgas.dictionaries.mapper.HktsFieldControlMapper;
import com.hkts.naturalgas.dictionaries.service.IHktsFieldControlService;
import com.hkts.naturalgas.utils.SnowFlakeUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author yck
 * @since 2021-10-25
 */
@Service
@Transactional
public class HktsFieldControlServiceImpl extends ServiceImpl<HktsFieldControlMapper, HktsFieldControl> implements IHktsFieldControlService {


    @Autowired
    private HktsFieldControlMapper hktsFieldControlMapper;
    @Autowired
    private HktsEnvironmentProtectionItemMapper hktsEnvironmentProtectionItemMapper;

    @Override
    public IPage<HktsFieldControl> selectPage(Page<HktsFieldControl> page, HktsFieldControl hktsFieldControl) {
        QueryWrapper<HktsFieldControl> query = Wrappers.query();
        QueryWrapper<HktsFieldControl> entityWrapper = getEntityWrapper(query, hktsFieldControl);
        return hktsFieldControlMapper.selectPage(page, entityWrapper);
    }


    @Override
    public List<HktsFieldControl> selectList(HktsFieldControl hktsFieldControl) {
        QueryWrapper<HktsFieldControl> query = Wrappers.query();
        QueryWrapper<HktsFieldControl> entityWrapper = getEntityWrapper(query, hktsFieldControl);
        return hktsFieldControlMapper.selectList(entityWrapper);
    }

    public HktsFieldControlMapper getHktsFieldControlMapper() {
        return hktsFieldControlMapper;
    }

    public void setHktsFieldControlMapper(HktsFieldControlMapper hktsFieldControlMapper) {


        this.hktsFieldControlMapper = hktsFieldControlMapper;
    }

    @Override
    public Map<String, Object> hanglerItem(List<HktsEnvironmentProtectionItem> controllList, Map<String, Object> map) {
        HktsFieldControl hktsFieldControl=new HktsFieldControl();
        List<HktsFieldControl> hktsFieldControls = this.selectList(hktsFieldControl);
        Map<String, HktsFieldControl> maps=hktsFieldControls.stream().collect(Collectors.toMap(HktsFieldControl::getItemId, Function.identity()));
        for(int i=0;i<controllList.size();i++){

        }
        return map;
    }

    @Override
    public void updateProtectionItem(HktsFieldControl filed) {
        QueryWrapper<HktsFieldControl> query = Wrappers.query();
        query.lambda().eq(HktsFieldControl::getItemId,filed.getItemId());
        HktsFieldControl hktsFieldControl = hktsFieldControlMapper.selectOne(query);
        if(hktsFieldControl==null){
            filed.setId(String.valueOf( SnowFlakeUtil.getFlowIdInstance().nextId()));
            this.saveOrUpdate(filed);
        }else{
            hktsFieldControl.setFixedValue(filed.getFixedValue());
            hktsFieldControl.setMaxValue(filed.getMaxValue());
            hktsFieldControl.setMinValue(filed.getMinValue());
            this.updateById(hktsFieldControl);
        }
        //设置字段needdeal
        HktsEnvironmentProtectionItem hktsEnvironmentProtectionItem = hktsEnvironmentProtectionItemMapper.selectById(filed.getItemId());
       if(filed.getFixedValue()!=null){
           hktsEnvironmentProtectionItem.setNeedDeal(EnviromentConstance.FIXED_VALUE);
       }else{
           hktsEnvironmentProtectionItem.setNeedDeal(EnviromentConstance.SECTION);
       }
        hktsEnvironmentProtectionItemMapper.updateById(hktsEnvironmentProtectionItem);
    }

    /**
     * 公共查询条件
     *
     * @param entityWrapper
     * @return
     */
    public QueryWrapper<HktsFieldControl> getEntityWrapper(QueryWrapper<HktsFieldControl> entityWrapper, HktsFieldControl hktsFieldControl) {
        //条件拼接
        if (StringUtils.isNotBlank(hktsFieldControl.getId())) {
            entityWrapper.like(HktsFieldControl.ID, hktsFieldControl.getId());
        }

        if (StringUtils.isNotBlank(hktsFieldControl.getItemId())) {
            entityWrapper.like(HktsFieldControl.ITEM_ID, hktsFieldControl.getItemId());
        }
        return entityWrapper;
    }

    @Override
    public HktsFieldControl selectFiledControllerByItemId(String itemId) {
        QueryWrapper<HktsFieldControl> query = Wrappers.query();
        query.lambda().eq(HktsFieldControl::getItemId,itemId);
        HktsFieldControl hktsFieldControl = hktsFieldControlMapper.selectOne(query);
        return hktsFieldControl;
    }
}
